/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.psi;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.xml.XmlTag;

import java.util.ArrayList;
import java.util.List;

/**
 * PSI Element that provides introspection functionality for components defined by external composites.
 */
public class SCAMockComponentTagPsiElement extends SCAComponentTagPsiElement {
    private final List<PsiClass> interfaces;
    private final GlobalSearchScope scope;

    public SCAMockComponentTagPsiElement(XmlTag underlying, XmlTag implTag) {
        super(underlying);

        // Find our current module
        Module currentModule = ModuleUtil.findModuleForPsiElement(this);
        scope = currentModule.getModuleWithDependenciesAndLibrariesScope(true);

        // Attempt to find the resource that defines us
        String text = implTag.getText();

        interfaces = new ArrayList<PsiClass>();
        for (String possibleIf : text.split("\\s")) {
            String cleanPossibleIf = possibleIf.trim();

           PsiClass interfaceClass = JavaPsiFacade.getInstance(getProject()).findClass(cleanPossibleIf, scope);
            if (interfaceClass != null) {
                interfaces.add(interfaceClass);
            }
        }
    }

    public PsiElement resolveServiceInterface(String serviceInterfaceName) {
        if (serviceInterfaceName.equals("IMocksControl")) {
            return JavaPsiFacade.getInstance(getProject()).findClass("org.easymock.IMocksControl", scope);
        }

        for (PsiClass interfaze : interfaces) {
            if (interfaze.getName().equals(serviceInterfaceName)) {
                return interfaze;
            }
        }

        return null;
    }

    public Object[] getVariantsOfServiceInterfaces(boolean qualified) {
        // Retrieve all the promotions for the referenced file
        String componentName = getComponentName();
        List<String> result = new ArrayList<String>();
        result.add(qualified ? componentName + "/IMocksControl" : "IMocksControl");
        for(PsiClass interfaze : interfaces) {
            result.add(qualified ? componentName + "/" + interfaze.getName() : interfaze.getName());
        }

        return result.toArray();
    }

    public PsiElement resolveReference(String referenceName) {
        return null;
    }

    public PromotedWireable[] getVariantsOfReferences(boolean qualified) {
        return new PromotedWireable[0];
    }

    @Override
    public PromotedWireable[] getVariantsOfProperties(boolean qualified) {
        return new PromotedWireable[0];
    }

    public PsiElement resolveProperty(String propertyName) {
        return null;
    }
}